/*
 * @lc app=leetcode.cn id=705 lang=javascript
 *
 * [705] 设计哈希集合
 */

// @lc code=start

var MyHashSet = function() {
  this.BASE = 769;
  this.data = new Array(this.BASE).fill(0).map(() => new Array());
};


/**
 * @param {number} key
 * @return {void}
 */
 MyHashSet.prototype.hash = function(key) {
  return key % this.BASE;
};

/**
 * @param {number} key
 * @return {void}
 */
MyHashSet.prototype.getIndex = function(key) {
  const h = this.hash(key);
  if (this.data[h]) {
    for(let i = 0; i < this.data[h].length; i++) {
      const item = this.data[h][i];
      if (item === key) {
        return i;
      }
    }
  }

  return -1;
};

/**
 * @param {number} key
 * @return {void}
 */
MyHashSet.prototype.add = function(key) {
  const h = this.hash(key);
  if (this.contains(key)) {
    return;
  }
  this.data[h] && this.data[h].push(key);
};

/**
 * @param {number} key
 * @return {void}
 */
MyHashSet.prototype.remove = function(key) {
  const h = this.hash(key);
  let index = this.getIndex(key);
  index !== -1 && this.data[h].splice(index, 1);
};

/**
 * @param {number} key
 * @return {boolean}
 */
MyHashSet.prototype.contains = function(key) {
  return this.getIndex(key) !== -1;
};

/**
 * Your MyHashSet object will be instantiated and called as such:
 * var obj = new MyHashSet()
 * obj.add(key)
 * obj.remove(key)
 * var param_3 = obj.contains(key)
 */
// @lc code=end

